import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/tools_base/widget/net_image_widget.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

import '../../../hj_model/video_model.dart';
import '../community_tag_page/community_tag_page.dart';

class CommunitySpecialView extends StatelessWidget {
  final List dataSource;

  const CommunitySpecialView(this.dataSource, {super.key});

  @override
  Widget build(BuildContext context) {
    if (dataSource.isEmpty) return SizedBox.shrink();
    return GridView.builder(
      physics: NeverScrollableScrollPhysics(),
      padding: EdgeInsets.only(left: 18.w, right: 18.w, bottom: 18.h),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        mainAxisSpacing: 7,
        crossAxisSpacing: 7,
        childAspectRatio: 126 / 57,
      ),
      itemCount: dataSource.length,
      shrinkWrap: true,
      itemBuilder: (BuildContext context, int index) {
        return InkWell(
          onTap: () {
            Get.to(CommunityTagDetailPage(), arguments: dataSource[index], preventDuplicates: true);
          },
          child: CommunitySectionCell(dataSource[index]),
        );
      },
    );
  }
}

class CommunitySectionCell extends StatelessWidget {
  final TagsBean model;
  final bool isDarkStyle;

  CommunitySectionCell(this.model, {this.isDarkStyle = false});

  @override
  Widget build(BuildContext context) {
    if (isDarkStyle) {
      return _buildDarklStyle();
    } else {
      return _buildNormalStyle();
    }
  }

  Widget _buildDarklStyle() {
    return Stack(
      fit: StackFit.expand,
      children: [
        Container(
          padding: EdgeInsets.only(bottom: 18),
          child: Stack(
            fit: StackFit.expand,
            children: [
              NetworkImageLoader(
                imageUrl: model.coverImg ?? '',
                borderRadius: 6,
                width: double.infinity,
                height: double.infinity,
              ),
              Container(
                decoration: BoxDecoration(borderRadius: BorderRadius.circular(6), color: Colors.black.withOpacity(.6)),
              ),
              if (model.hotMark?.isNotEmpty == true)
                Positioned(
                  top: 0,
                  left: 0,
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 3, vertical: 1),
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        colors: [
                          Color.fromRGBO(252, 118, 118, 1),
                          Color.fromRGBO(212, 39, 39, 1),
                        ],
                      ),
                      borderRadius: BorderRadius.only(
                        bottomRight: Radius.circular(6),
                        topLeft: Radius.circular(6),
                      ),
                    ),
                    child: Text(
                      "${model.hotMark}",
                      style: TextStyle(color: Colors.white, fontSize: 11),
                    ),
                  ),
                ),
            ],
          ),
        ),
        Container(
          padding: EdgeInsets.symmetric(vertical: 8.h),
          child: Stack(
            fit: StackFit.expand,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    "${model.tagName}",
                    style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold),
                  ),
                  2.sizeBoxH,
                  Text(
                    "${model.videoCount.humanizeFormat}个帖子",
                    style: TextStyle(color: Colors.white, fontSize: 12),
                  ),
                ],
              ),
              if (model.hotMark?.isNotEmpty == true)
                Positioned(
                  top: 0,
                  left: 0,
                  child: Container(
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        colors: [
                          Color.fromRGBO(252, 118, 118, 1),
                          Color.fromRGBO(212, 39, 39, 1),
                        ],
                      ),
                      borderRadius: BorderRadius.only(
                        bottomRight: Radius.circular(6),
                        topLeft: Radius.circular(6),
                      ),
                    ),
                    width: 26,
                    height: 14,
                    alignment: Alignment.center,
                    child: Text(
                      "${model.hotMark}",
                      style: TextStyle(color: Colors.white, fontSize: 11),
                    ),
                  ),
                ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildNormalStyle() {
    return Stack(
      fit: StackFit.expand,
      children: [
        NetworkImageLoader(
          imageUrl: model.coverImg ?? '',
          borderRadius: 6,
          width: double.infinity,
          height: double.infinity,
        ),
        Container(
          decoration: BoxDecoration(
            color: Color.fromRGBO(0, 0, 0, 0.6),
            borderRadius: BorderRadius.circular(6),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "${model.tagName}",
                style: TextStyle(color: Colors.white, fontSize: 15),
              ),
              2.sizeBoxH,
              Text(
                "${model.videoCount.humanizeFormat}个帖子",
                style: TextStyle(color: Colors.white, fontSize: 12),
              ),
            ],
          ),
        ),
        if (model.hotMark?.isNotEmpty == true)
          Positioned(
            top: 0,
            left: 0,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 1, vertical: 1),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [
                    Color.fromRGBO(252, 118, 118, 1),
                    Color.fromRGBO(212, 39, 39, 1),
                  ],
                ),
                borderRadius: BorderRadius.only(
                  bottomRight: Radius.circular(6),
                  topLeft: Radius.circular(6),
                ),
              ),
              child: Text(
                "${model.hotMark}",
                style: TextStyle(color: Colors.white, fontSize: 10),
              ),
            ),
          ),
      ],
    );
  }
}
